package com.kingdee.eas.custom.aoyuan.aytime.bizbill;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineCollection;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineFactory;
import com.kingdee.eas.custom.aoyuan.aytime.basedata.MachineInfo;

public class NetWorkTimeMachine implements IMachineFetch {

	public ArrayList FetchRecords(Date startDate, Date endDate, Map params) {
		ArrayList ar = new ArrayList();
		try {
			String ipAgentAddress=(String)params.get("Ipaddress");
			Integer pAgentport=(Integer)params.get("port");
			String deviceName=(String)params.get("InstanceName");
			String deviceNum=(String)params.get("OtherUrl3");
			String password=(String)params.get("Password");
			String license=(String)params.get("Username");
			String machineIPAddress =(String)params.get("OtherUrl1");
			String machinePort =(String)params.get("OtherUrl2");

			Socket socket = new Socket(ipAgentAddress, pAgentport);
			BufferedReader in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

			String strSQL = machineIPAddress + "|" + machinePort + "|"
					+ deviceName + "|" + deviceNum + "|" + password + "|"
					+ license + "|";
			int outLen = 7 + strSQL.length();
			String sOutLen = String.valueOf(outLen);
			sOutLen = "000000" + sOutLen;
			sOutLen = sOutLen.substring(sOutLen.length() - 6);
			strSQL = sOutLen + "|" + strSQL;
			// out.write(strSQL);
			out.println(strSQL);
			out.flush();
			// line.close();
			String result = in.readLine();
			out.close();
			in.close();
			socket.close();

			if (StringUtils.isNotEmpty(result)) {
				String[] resArray = result.split("\\|");
				if (resArray.length > 2) {
					// 表示有打卡记录
					for (int i = 2; i < resArray.length; i++) {
						// 格式1000;2012-10-12 11:40:36Z
						String cardResult = resArray[i];
						String[] cardArray = cardResult.split(";");
						SimpleCardRecord card = new SimpleCardRecord();
						card.eEmpID = cardArray[0];
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
						card.sDate = df.parse(cardArray[1].substring(0, 10));
						card.sTime = cardArray[1].substring(11, 19);
						ar.add(card);
					}
					return ar;
				} else {
					return ar;
				}
			} else {
				return ar;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return ar;
		}
	}

}
